package jdbcUtils;

import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class mysqljdbcconnection {
    private static String url;
    private static String name;
    private static String password;
    //读取文件内容
    static {
        InputStream inputStream=readdb.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties=new Properties();
        try {
            properties.load(inputStream);
            properties.getProperty(url);
            properties.getProperty(password);
            properties.getProperty(name);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public Connection getConnection(){

        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl(url);
        ((MysqlDataSource)dataSource).setUser(name);
        ((MysqlDataSource)dataSource).setPassword(password);
        try {
            Connection connection=dataSource.getConnection();
            return connection;
           // PreparedStatement preparedStatement= connection.prepareStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
